<?php
/**
 * PostController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Feed
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * Feed_PostController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Feed
 */
class Feed_PostController extends Madoqua_Controller_Action_Abstract
{
    /**
     * Post model
     *
     * @var Blog_Post
     */
    private $_postModel;

    /**
     * Comments model
     *
     * @var Blog_Comment
     */
    private $_commentModel;

    /**
     * initialization
     */
    public function init()
    {
        parent::init();

        $this->_modelLoader->load(array('Blog_Post', 'Blog_Comment'));
        $this->_postModel = new Blog_Post();
        $this->_commentModel = new Blog_Comment();
        //set up model

        $type = trim($this->getRequest()->getParam('format', 'rss'));
        //format type

        $this->view->type = 'rss';
        if (in_array($type, array('rss', 'atom'))) {
            $this->view->type = $type;
        }
        //set the relevant type
    }

    /**
     * index action, displays feeds for all post
     */
    public function indexAction()
    {
        $count = (int) $this->getRequest()->getParam('count', 10);
        //post count

        $posts = $this->_postModel->fetchAll(null, 'id desc', $count);
        //FIXME move to model

        $this->view->posts = $posts;
    }

    /**
     * feed for a single post
     *
     */
    public function singleAction()
    {
        $id = (int) $this->_getParam('id');
        //slug and/or id from the params

        if (!empty($id)) {
            $post = $this->_postModel->fetchById($id);
        } else {
            throw new Exception('No id, can\'t create feed');
        }
        //find post using slug or id and throw an exception if both aren't set

        $comments = $this->_commentModel->fetchByPost($post);
        //select all accepted comments for $post

        $this->view->post = $post;
        $this->view->comments = $comments;
    }
}